<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="分类名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：友情合作"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" prop="description">
        <el-input class="w500" v-model="form.description" placeholder="例如：这是合作方的文章"></el-input>
      </el-form-item>
      <el-form-item label="选择所属应用">
        <el-select v-model="form.key" placeholder="请选择">
          <el-option v-for="item in applistall" :key="item.app_key" :label="item.name" :value="item.app_key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序">
        <el-input class="w100" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        rules: {
          name: [
            { required: true, message: '请输入文章分类名称', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapState({
        applistall: state => state.app.all,
        form: state => state.article.saveData
      })
    },
    created () {
      this.appAll()
    },
    methods: {
      // 获取不分页的应用列表
      async appAll () {
        await this.$store.dispatch('app/appAll')
      },
      onSubmit (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('article/classEdit', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/article/articleclasstree')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
